<template>
	<a-range-picker
		v-if 			= "type === 'date-range'" 
		style 			= 'width:100%' 
		:style 			= '{width:vn.setPx(width)}'
		v-model:value 	= 'innerValue' 
		:show-time 		= 'showTime' 
		:size 			= 'size' 
		:format 		= 't' 
		:placehoder 	= 'p'
		:allowClear 	= 'allowClear'
		:disabledDate 	= 'disabledDate'
		:allowEmpty 	= '[true,true]'
		@change 		= 'onChange' 
		@ok 			= 'onOk'
	/>
	<a-date-picker 
		v-else 
		style 			= 'width:100%'
		:style 			= '{width:vn.setPx(width)}'
		v-model:value 	= 'innerValue' 
		:show-time 		= 'showTime' 
		:size 			= 'size' 
		:format 		= 't'
		:placehoder 	= 'p'
		@change 		= 'onChange' 
		:allowClear 	= 'allowClear'
		:disabledDate 	= 'disabledDate'
		@ok 			= 'onOk'
	/>
</template>

<script>
	import { DatePicker } from 'ant-design-vue'
	import { ref, watch } from 'vue'
	app.use(DatePicker)
	/* --------------------------- monent --------------------------- */
	import moment from 'moment'
	/* --------------------------- data --------------------------- */
	export default{
		props:{
			value 			: [Array, String],
			name 			: String,
			label 			: String,
			form 			: Object,
			p				: { type:String },
			size			: String,
			allowClear 		: { type:Boolean, default:true },
			format			: { type:Number, default: 0 },
			s				: { type:String, default:'-' },
			name 			: String,
			start			: [String, Number],
			end				: { type:[String, Number], default: '23:59:59' },
			after 			: { Boolean, Number, String },
			before 			: { Boolean, Number, String },
			today 			: [ Boolean, Number ],
			type 			: String,
			width 			: [String, Number ],
		},
		emits:{
			change:null,
			'update:value':null
		},
		setup(props, ctx){
			let { format, type, value, s, start, end, after, before, today } = props
			
			const formatType = [
				`YYYY${s}MM${s}DD`,
				`YYYY${s}MM${s}DD HH:mm:ss`,
				`YYYY${s}MM${s}DD HH:mm`,
			]
			
			const t = $fn.isNumber(format) ? formatType[format] : format
			
			const innerValue = ref(value ? moment(value,t) : null)
			
			const isRange = type === 'date-range'
			
			// 时分秒的默认值
			let showTime
			const isShowTime = formatType[format].indexOf('HH') !== -1
			// date-range
			if(isRange){
				value = value ? value : []
				innerValue.value = value[0]||value[1] ? [moment(value[0],t), moment(value[1],t)] : null
				if( isShowTime ){
					if( start === 0){ start = '00:00:00' }
					if( end === 0){ end = '00:00:00' }
					
					const timeStart = start ? moment(start, 'HH:mm:ss') : null
					const timeEnd = end ? moment(end, 'HH:mm:ss') : null
					
					showTime = { defaultValue: [ timeStart, timeEnd ]  }
				}
			}else{
				if(isShowTime){
					if( start === 0){ start = '00:00:00' }
					showTime = start ? { defaultValue:moment(start, 'HH:mm:ss') } : true
				}
			}
			
			watch(()=>props.value, value =>{
				if(isRange){
					if(value.length ===1 && value[0] === undefined){
						innerValue.value = null
					}
				}else{
					innerValue.value = value ? moment(props.value,t) : null
				}
			})
		
			return {
				vn: $,
				t,
				innerValue,
				showTime,
				onChange: (value, dateString) => {
					ctx.emit('update:value',dateString)
					if(format === 2 || value.length === 0){
						ctx.emit('change', $fn.getFormFormat({...props, value:dateString, formType:'text' }))
					}
				},
				onOk: (value, dateString) => {
					if(format !== 2){
						// ctx.emit('change', dateString)
						ctx.emit('change', $fn.getFormFormat({...props, value:dateString, formType:'text' }))
					}
				},
				// 禁用时间
				disabledDate: current => {
					if( after ){
						const  hasToday = !(today !== false && today !== 0)
						if( typeof after === 'string' ){
							const now = moment(after) + (hasToday ? 8.64e7 : 0)
							return current && (current <= now)
						}else{
							const now = Date.now() - (hasToday ? 0 : 8.64e7)
							return current && (current <= now)
						}
					}
					if( before ){
						const hasToday = !(today !== false && today !== 0)
						if( typeof before === 'string' ){
							const now = moment(before) + (hasToday ? 0 : 8.64e7)
							return current && (current > now)
						}else{
							const now = Date.now() - (hasToday ? 8.64e7 : 0)
							return current && (current >= now)
						}
					}
				}
			}
		}
	}
</script>